package com.yf.exam.modules.trainingarchives.trainingclassschedule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.exam.common.enums.DeleteCodeEnum;
import com.yf.exam.common.vo.PullDownListVO;
import com.yf.exam.common.vo.TraingTypeAndDescVO;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.trainingarchives.pdfexport.dto.second.TrainingClassScheduleExportPdfDTO;
import com.yf.exam.modules.trainingarchives.pdfexport.dto.second.three.TrainingClassScheduleListExportDTO;
import com.yf.exam.modules.trainingarchives.trainingclassinfo.entity.TrainingClassInfo;
import com.yf.exam.modules.trainingarchives.trainingclassinfo.mapper.TrainingClassInfoMapper;
import com.yf.exam.modules.trainingarchives.trainingclassschedule.dto.export.TrainingClassScheduleExportDTO;
import com.yf.exam.modules.trainingarchives.trainingclassschedule.dto.req.TrainingClassScheduleReqDTO;
import com.yf.exam.modules.trainingarchives.trainingclassschedule.dto.resp.TrainingClassScheduleRespDTO;
import com.yf.exam.modules.trainingarchives.trainingclassschedule.entity.TrainingClassSchedule;
import com.yf.exam.modules.trainingarchives.trainingclassschedule.mapper.TrainingClassScheduleMapper;
import com.yf.exam.modules.trainingarchives.trainingclassschedule.service.ITrainingClassScheduleService;
import io.swagger.annotations.ApiModelProperty;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 培训班课程安排表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-29
 */
@Service
public class TrainingClassScheduleServiceImpl extends ServiceImpl<TrainingClassScheduleMapper, TrainingClassSchedule> implements ITrainingClassScheduleService {

    @Resource
    private TrainingClassInfoMapper trainingClassInfoMapper;

    /**
     * 添加培训班课程安排信息
     * @param reqDTO
     * @return
     */
    @Override
    public Boolean add(TrainingClassScheduleReqDTO reqDTO) {
        TrainingClassSchedule trainingClassSchedule = new TrainingClassSchedule();
        BeanUtils.copyProperties(reqDTO, trainingClassSchedule);
        return this.save(trainingClassSchedule);
    }

    /**
     *  删除培训班课程安排信息
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(String id) {
        LambdaQueryWrapper<TrainingClassSchedule> lambdaQueryWrapper = new LambdaQueryWrapper<TrainingClassSchedule>();
        lambdaQueryWrapper.eq(TrainingClassSchedule::getId, id);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 修改培训班课程安排信息
     * @param reqDTO
     * @return
     */
    @Override
    public Boolean updateTrainingclassschedule(TrainingClassScheduleReqDTO reqDTO) {
        TrainingClassSchedule trainingClassSchedule = new TrainingClassSchedule();
        BeanUtils.copyProperties(reqDTO, trainingClassSchedule);
        return this.updateById(trainingClassSchedule);
    }

    /**
     * 分页查询
     * @param reqDTO
     * @return
     */
    @Override
    public IPage<TrainingClassScheduleRespDTO> paging(PagingReqDTO<TrainingClassScheduleReqDTO> reqDTO) {
        return baseMapper.paging(reqDTO.toPage(),reqDTO.getParams());
    }


    @Override
    @SneakyThrows
    public TrainingClassScheduleRespDTO getDetail(BaseIdReqDTO baseIdReqDTO) {
        TrainingClassScheduleRespDTO trainingClassScheduleRespDTO = new TrainingClassScheduleRespDTO();
        TrainingClassSchedule byId = this.getById(baseIdReqDTO.getId());
        BeanUtils.copyProperties(byId,trainingClassScheduleRespDTO);
        return trainingClassScheduleRespDTO;
    }

    @Override
    public void importExcel(String id,List<TrainingClassScheduleExportDTO> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        // 批量插入
        list.forEach(item ->{
            TrainingClassScheduleReqDTO reqDTO = new TrainingClassScheduleReqDTO();
            BeanUtils.copyProperties(item,reqDTO);
//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
//            LocalDateTime localDateTime = LocalDateTime.parse(item.getTrainingTime(), formatter);
//            reqDTO.setTrainingTime(localDateTime);
            reqDTO.setTrainingClassInfoId(id);
            this.add(reqDTO);
        });
    }

    @Override
    public List<TrainingClassScheduleRespDTO> getList() {
        List<TrainingClassSchedule> list = this.list();
        List<TrainingClassScheduleRespDTO> respDTOList = new ArrayList<>();
        list.forEach(item ->{
            TrainingClassScheduleRespDTO trainingClassScheduleRespDTO = new TrainingClassScheduleRespDTO();
            BeanUtils.copyProperties(item, trainingClassScheduleRespDTO);
            respDTOList.add(trainingClassScheduleRespDTO);
        });
        return respDTOList;
    }


    /**
     * 获取培训班课程安排表pdf导出内容
     * @param trainingClassId
     * @return
     */
    @Override
    public TrainingClassScheduleExportPdfDTO getPdfData(String trainingClassId) {
        TrainingClassScheduleExportPdfDTO trainingClassScheduleExportDTO = new TrainingClassScheduleExportPdfDTO();
        TrainingClassInfo trainingClassInfo = trainingClassInfoMapper.selectById(trainingClassId);
        trainingClassScheduleExportDTO.setPxbkcapTrainingClassScheduleName(trainingClassInfo.getClassName());
        List<TrainingClassSchedule> list = this.list(new LambdaQueryWrapper<TrainingClassSchedule>().eq(TrainingClassSchedule::getTrainingClassInfoId, trainingClassId)
                .eq(TrainingClassSchedule::getIsDeleted, 0));
        List<TrainingClassScheduleListExportDTO> trainingClassScheduleListExportDTOList = new ArrayList<>();
        list.forEach(item ->{
            TrainingClassScheduleListExportDTO trainingClassScheduleListExportDTO = new TrainingClassScheduleListExportDTO();
            trainingClassScheduleListExportDTO.setPxbkcapTeacherName(item.getTeacherName());
            trainingClassScheduleListExportDTO.setPxbkcapEducation(item.getEducation());
            trainingClassScheduleListExportDTO.setPxbkcapMajor(item.getMajor());
            trainingClassScheduleListExportDTO.setPxbkcapTitle(item.getTitle());
            trainingClassScheduleListExportDTO.setPxbkcapCourse(item.getCourse());
            trainingClassScheduleListExportDTO.setPxbkcapHours(item.getHours());
            trainingClassScheduleListExportDTO.setPxbkcapQualificationNumber(item.getQualificationNumber());
            trainingClassScheduleListExportDTOList.add(trainingClassScheduleListExportDTO);
        });
        trainingClassScheduleExportDTO.setTrainingClassScheduleListExportDTOList(trainingClassScheduleListExportDTOList);
        return trainingClassScheduleExportDTO;
    }

    @Override
    public List<PullDownListVO> getPullDownList(String id) {
        List<TrainingClassSchedule> list = this.list(Wrappers.<TrainingClassSchedule>lambdaQuery().eq(TrainingClassSchedule::getTrainingClassInfoId,id).eq(TrainingClassSchedule::getIsDeleted, DeleteCodeEnum.UN_DELETE.code));
        List<PullDownListVO> respDTOList = new ArrayList<>();
        list.forEach(item ->{
            PullDownListVO pullDownListVO = new PullDownListVO();
            pullDownListVO.setId(item.getId());
            pullDownListVO.setName((item.getTeacherName()!=null ? item.getTeacherName():"暂无教师")+"-"+item.getCourse());
            respDTOList.add(pullDownListVO);
        });
        return respDTOList;
    }

    @Override
    public List<TrainingClassScheduleRespDTO> getlist() {
        List<TrainingClassSchedule> list = this.list();
        return BeanUtil.copyToList(list, TrainingClassScheduleRespDTO.class);
    }

    @Override
    public List<PullDownListVO> getListById(String id) {
        List<TrainingClassSchedule> list = this.list(Wrappers.<TrainingClassSchedule>lambdaQuery()
                .eq(TrainingClassSchedule::getTrainingClassInfoId,id)
                .eq(TrainingClassSchedule::getIsDeleted, DeleteCodeEnum.UN_DELETE.code));
        List<PullDownListVO> respDTOList = new ArrayList<>();
        list.forEach(item ->{
            PullDownListVO pullDownListVO = new PullDownListVO();
            pullDownListVO.setId(item.getId());
            pullDownListVO.setName(item.getCourse());
            respDTOList.add(pullDownListVO);
        });
        return respDTOList;
    }

    @Override
    public TraingTypeAndDescVO getDesc(String id) {
        return trainingClassInfoMapper.getDesc(id);
    }

}
